package org.testfire.realm.control.testfire.queue;

import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import org.apache.log4j.Logger;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.control.user.UserBean;
import org.testfire.realm.model.entities.testfire.AgentEntity;
import org.testfire.realm.model.entities.testfire.TestRunnerQueueEntity;
import org.testfire.realm.model.entities.testfire.TestsetEntity;
import org.testfire.realm.util.MapStoreBean;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Aug 22, 2007
 * Time: 2:06:59 PM
 * <p/>
 * Request managed bean.
 */
public class QueueBean
{
    // Inject the lgsrealm entity manager, as defined in the persistence.xml file.
    @PersistenceContext(unitName = "testfire")
    EntityManager em;

    @Resource
    private UserTransaction utx;

    /* @Managed */
    private TestFireDBAO testfireDBAO;

    private UserBean userBean;

    private MapStoreBean mapStoreBean;

    TestRunnerQueueEntity queue = null;

    List<TestsetEntity> testsets = null;

    private List<TestsetEntity> availableTestsets;

    private List<TestRunnerQueueEntity> queues;

    private List<TestsetEntity> enqueuedTestsets;

    private List<AgentEntity> availableAgents;

    private List<AgentEntity> inUseAgents;

    Logger log = Logger.getLogger( QueueBean.class );

    public TestRunnerQueueEntity getQueue()
    {
        if ( queue == null )
        {
            try
            {
                boolean needTransaction = utx.getStatus() == Status.STATUS_NO_TRANSACTION;
                if ( needTransaction )
                {
                    utx.begin();
                }
                this.queue = em.merge( testfireDBAO.getTestRunnerQueueEntity( "default_queue" ) );
                if ( needTransaction )
                {
                    utx.commit();
                }
            }
            catch ( Exception ex )
            {
                log.error( "Could not merge queue entity.", ex );
            }
        }
        return queue;
    }

    public TestRunnerQueueEntity getQueue( Integer id )
    {
        if ( queue == null )
        {
            try
            {
                this.queue = em.merge(em.find( TestRunnerQueueEntity.class, id ));
            }
            catch ( Exception ex )
            {
                log.error( "Could not find queue entity with id '" + id + "'.", ex );
            }
        }
        return queue;
    }

    public List<TestRunnerQueueEntity> getQueues()
    {
        if ( queues == null )
        {
            this.queues = testfireDBAO.getQueues();
        }
        return queues;
    }

    public List<TestsetEntity> getTestsets()
    {
        if ( this.enqueuedTestsets == null )
        {
            Integer id = (Integer)mapStoreBean.get( "queueId" );
            if ( id != null )
            {
                this.enqueuedTestsets = getQueue( id ).getTestsets();
            }
            else
            {
                this.enqueuedTestsets = getQueue().getTestsets();
            }
        }
        return this.enqueuedTestsets;
    }

    public List<TestsetEntity> getAvailableTestsets()
    {
        if ( this.availableTestsets == null )
        {
            Integer id = (Integer)mapStoreBean.get( "queueId" );
            if ( id != null )
            {
                this.availableTestsets = testfireDBAO.getAvailableTestsets( getQueue( id ) );
            }
            else
            {
                this.availableTestsets = testfireDBAO.getAvailableTestsets( getQueue() );
            }

        }
        return this.availableTestsets;
    }

    public List<AgentEntity> getAgents()
    {
        if ( this.inUseAgents == null )
        {
            Integer id = (Integer)mapStoreBean.get( "queueId" );
            if ( id != null )
            {
                this.inUseAgents = getQueue( id ).getAgents();
            }
            else
            {
                this.inUseAgents = getQueue().getAgents();
            }
        }
        return this.inUseAgents;
    }

    public List<AgentEntity> getAvailableAgents()
    {
        if ( this.availableAgents == null )
        {
            Integer id = (Integer)mapStoreBean.get( "queueId" );
            if ( id != null )
            {
                this.availableAgents = TestFireDBAO.getAvailableAgents( getQueue( id ), em );
            }
            else
            {
                this.availableAgents = TestFireDBAO.getAvailableAgents( getQueue(), em );
            }

        }
        return this.availableAgents;
    }

    public TestFireDBAO getTestfireDBAO()
    {
        return testfireDBAO;
    }

    public void setTestfireDBAO( TestFireDBAO testfireDBAO )
    {
        this.testfireDBAO = testfireDBAO;
    }

    public UserBean getUserBean()
    {
        return userBean;
    }

    public void setUserBean( UserBean userBean )
    {
        this.userBean = userBean;
    }

    public void setMapStoreBean( MapStoreBean mapStoreBean )
    {
        this.mapStoreBean = mapStoreBean;
    }
}